package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 * @author Xiao
 * @date 2021/9/27
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    @ApiOperation("新建套餐接口")
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto) {
        log.info("setmealDto:{}",setmealDto.toString());
        setmealService.addSetmeal(setmealDto);
        return R.success("添加成功！");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    @ApiOperation("套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true),
            @ApiImplicitParam(name = "name", value = "套餐名称")
    })
    public R<Page<SetmealDto>> getSetmealPage(@RequestParam Integer page, Integer pageSize, String name) {
        log.info("page:{}, pageSize:{}, name:{}", page, pageSize, name);
        Page<SetmealDto> setmealDtoPage = setmealService.getSetmealPage(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    /**
     * (批量)删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)  //清除setmealCache名称下,所有的缓存数据
    @ApiOperation("(批量)删除套餐接口")
    @ApiImplicitParam(name = "ids", value = "所有要删除的套餐id", required = true)
    public R<String> deleteSetmeal(@RequestParam List<Long> ids) {
        boolean result = setmealService.deleteSetmeal(ids);
        if (!result) {
//            return R.error("删除失败，在售状态下无法删除！");
            throw new CustomException("删除失败，在售状态下无法删除！");
        }
        return R.success("删除成功！");
    }

    /**
     * (批量)修改状态
      * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("(批量)修改套餐状态接口")
    @ApiImplicitParam(name = "ids", value = "所有要修改的套餐id", required = true)
    public R<String> setmealStatusByStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        log.info("status:{}, ids:{}", status, ids);
        setmealService.setmealStatusByStatus(status, ids);
        return R.success("修改成功！");
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐接口")
    public R<SetmealDto> querySetmealById(@PathVariable Long id) {
        log.info("setmealId:{}", id);
        SetmealDto setmealDto = setmealService.querySetmealById(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐接口")
    public R<String> editSetmeal(@RequestBody SetmealDto setmealDto) {
        log.info("setmealDto:{}", setmealDto);
        setmealService.editSetmeal(setmealDto);
        return R.success("修改成功！");
    }

    /**
     * 根据分类id和状态查询套餐
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id和状态查询套餐接口")
    public R<List<SetmealDto>> setmealListApi(Setmeal setmeal) {
        List<SetmealDto> setmealDtos = setmealService.setmealListApi(setmeal);
        return R.success(setmealDtos);
    }

    /**
     * 获取套餐详情
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    @ApiOperation("根据id获取套餐详情接口")
    public R<List<SetmealDishDto>> setMealDishDetailsApi(@PathVariable Long id) {
        log.info("id:{}", id);
        List<SetmealDishDto> setmealDishDtos = setmealService.setMealDishDetailsApi(id);
        return R.success(setmealDishDtos);
    }
}
